import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/alert_business/video/video_line_menu_alert.dart';
import 'package:tiktokadult/hj_page/home_business/tag/video_tag_page.dart';
import 'package:tiktokadult/hj_page/user_center_page/user_center_page.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:video_player/video_player.dart';
import 'package:tiktokadult/tools_base/widget/net_image_widget.dart';

import '../../../config/address.dart';
import '../../../hj_model/video_model.dart';
import '../../darkweb/widget/shrink_wrap.dart';

class VideoDetailInfoWidget extends StatelessWidget {
  final VideoModel? model;
  final VideoPlayerController? controller;

  const VideoDetailInfoWidget({
    super.key,
    this.model,
    this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildRichText(),
          _buildTags(),
          _buildUserInfo(),
        ],
      ),
    );
  }

  Widget _buildRichText() {
    if (model?.title?.trim().isNotEmpty == true)
      return Container(
        padding: EdgeInsets.only(top: 12),
        child: Text(
          model?.title?.trim() ?? "",
          maxLines: 2,
          style: TextStyle(color: Color(0xE5FFFFFF), height: 1.5, fontSize: 16, fontWeight: FontWeight.w500),
        ),
      );
    return SizedBox();
  }

  Widget _buildUserInfo() {
    return Container(
      margin: EdgeInsets.only(top: 18),
      height: 36,
      alignment: Alignment.centerLeft,
      child: InkWell(
        onTap: () {
          Get.toNamed(UserCenterPage.routeName, arguments: {'uid': model?.publisher?.uid ?? 0});
        },
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            NetworkImageLoader(
              imageUrl: model?.publisher?.portrait,
              width: 36,
              height: 36,
              borderRadius: 20,
            ),
            12.sizeBoxW,
            Expanded(
              child: Text(
                "${model?.publisher?.name ?? ""}",
                style: TextStyle(
                  color: Color(0xffEFEFEF),
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            12.sizeBoxW,
            Text(
              "作品 ${model?.publisher?.totalWorks ?? ""}",
              style: TextStyle(
                color: Color(0x8CFFFFFF),
                fontSize: 12,
              ),
            ),
            6.sizeBoxW,
            12.lineV,
            6.sizeBoxW,
            Text(
              "粉丝 ${model?.publisher?.fans?.humanizeFormat ?? "0"}",
              style: TextStyle(
                color: Color(0x8CFFFFFF),
                fontSize: 12,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTags() {
    if (model?.tags?.isNotEmpty == true) {
      return Container(
        padding: EdgeInsets.only(top: 12),
        child: ShrinkWrap(
          spacing: 0,
          runSpacing: 6,
          maxLines: 1,
          children: model!.tags!.map((e) => _buildTagItem(e)).toList(),
        ),
      );
    }
    return const SizedBox();
  }

  Widget _buildTagItem(TagsBean tag) {
    return InkWell(
      onTap: () {
        controller?.pause();
        Get.to(VideoTagPage(tag), preventDuplicates: true);
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(8, 2, 8, 2),
        margin: EdgeInsets.only(right: 6),
        decoration: BoxDecoration(
          color: Color(0x1AFFFFFF),
          borderRadius: BorderRadius.circular(3),
        ),
        child: Text(
          "#${tag.name}",
          style: TextStyle(
            color: Color(0xE5FFFFFF),
            fontSize: 11,
          ),
        ),
      ),
    );
  }
}

class VideoDetailStaticsCount extends StatelessWidget {
  final VideoModel? model;
  final Color? textColor;

  VideoDetailStaticsCount({
    this.model,
    this.textColor,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        if (model?.videoType != 1) ...[
          Image.asset(
            "hot_grey.png".videoPath,
            width: 14,
            height: 14,
          ),
          2.sizeBoxW,
          _buildCountText(model?.hot?.humanizeFormat),
          10.sizeBoxW,
          _buildCountText("/", fontSize: 10),
          10.sizeBoxW,
        ],
        Image.asset(
          "play_grey.png".videoPath,
          width: 14,
          height: 14,
        ),
        2.sizeBoxW,
        _buildCountText(model?.playCount?.humanizeFormat),
        10.sizeBoxW,
        _buildCountText("/", fontSize: 10),
        10.sizeBoxW,
        _buildCountText(model?.playTime?.timeMSFormat),
        10.sizeBoxW,
        if (model?.freeArea == true || model?.permission == 2)
          SizedBox() // 免费
        else if (model?.isCoinVideo() == true || model?.permission == 1) ...[
          _buildCountText("/", fontSize: 10),
          10.sizeBoxW,
          _buildCountText("金币"),
          10.sizeBoxW,
        ] else ...[
          _buildCountText("/", fontSize: 10),
          10.sizeBoxW,
          _buildCountText("VIP"),
          10.sizeBoxW,
        ],
        if (Address.cdnAddressLists.isNotEmpty) ...[
          _buildCountText("/", fontSize: 10),
          10.sizeBoxW,
          InkWell(
            onTap: () {
              VideoLineMenuAlert.show();
            },
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Image.asset(
                  "line_white.png".videoPath,
                  width: 14,
                  height: 14,
                ),
                2.sizeBoxW,
                _buildCountText(Address.currentDomain?.desc ?? ""),
              ],
            ),
          ),
        ],
      ],
    );
  }

  Text _buildCountText(String? title, {double? fontSize}) {
    return Text(
      title ?? "",
      style: TextStyle(
        color: textColor ?? Colors.white.withOpacity(0.6),
        fontSize: fontSize ?? 12,
      ),
    );
  }
}
